﻿// Copyright (c) JosephZheng. All rights reserved. See License.txt in the project root for license

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lyra.Cache.Client;
using Lyra.Cache.Client.Attribute;
using Lyra.Cache.Server.Interface.Contract;
using Lyra.Cache.Server.Interface.Contract.ClientResult;
using Lyra.Cache.Server.Interface.Contract.ServerResult;
using Newtonsoft.Json.Linq;

namespace Lyra.Cache.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            /*

            CacheClient client = new CacheClient(new string[] { "net.tcp://localhost:48088//cache" }, Client.Enum.RAIM.Mode5);
            //create a region named 'member' for cache
            client.Create("member", typeof(TestCacheItem));

            //add two cache item into 'member'
            client.Add("member", "lily", new TestCacheItem() { Name = "lily", Age = 30, Birth = DateTime.Now }, TimeSpan.FromMinutes(30));
            client.Add("member", "kate", new TestCacheItem() { Name = "kate", Age = 25, Birth = DateTime.Now }, TimeSpan.FromMinutes(30));

            //get use key
            GetClientResult<TestCacheItem,GetServerResult> result = client.Get<TestCacheItem>("member", "lily");
            if (result.HasError)
            {
                //get has some error,lookup for cache server's result
                foreach (KeyValuePair<string, GetServerResult> kvp in result.ServerResults)
                {
                    Console.WriteLine(string.Concat("server:", kvp.Key, " code:", kvp.Value.Code));
                }
            }
            TestCacheItem resultitem = result.Object.Data;
            //when the cache item cached
            DateTime? resulttime = result.Object.Time;

            //get use filter
            GetWithFilterClientResult<TestCacheItem,GetWithFilterServerResult> results = client.GetWithFilter<TestCacheItem>("member", "Age < 28");
            //get use lock and filter
            string locksession = Guid.NewGuid().ToString("N");
            GetWithFilterClientResult<TestCacheItem, GetWithFilterServerResult> resultlockeds = client.GetWithFilter<TestCacheItem>("member", "Age < 28", locksession, 30);
            //remove for process
            foreach (KeyValuePair<string, CacheObject<TestCacheItem>> kvp in resultlockeds.Objects)
            {
                client.Remove("member", kvp.Key, locksession);
            }
            */

            //test for extension
            CacheClient client = new CacheClient(new string[] { "net.tcp://localhost:48088//cache" }, Client.Enum.RAIM.Mode5);
            Dictionary<string, string> extensions = new Dictionary<string, string>();
            extensions.Add("findname",
                @"
                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using Lyra.Cache.Extension;

                namespace Lyra.Cache.Extension.Test
                {
                    public class TestExtension:IExtension
                    {
                        public bool Filter(System.Data.DataRow row, string extensionValue)
                        {
                            return row[""Name""].ToString().StartsWith(extensionValue);
                        }
                    }
                }"
                );

            ClientResult<ServerResult> result0 = client.Create("member", typeof(TestCacheItem), null, extensions);

            //add two cache item into 'member'
            client.Add("member", "lily", new TestCacheItem() { Name = "lily", Age = 30, Birth = DateTime.Now }, TimeSpan.FromMinutes(30));
            client.Add("member", "kate", new TestCacheItem() { Name = "kate", Age = 25, Birth = DateTime.Now }, TimeSpan.FromMinutes(30));
            //get with extension
            GetWithExtensionClientResult<TestCacheItem, GetWithExtensionServerResult> result = client.GetWithExtension<TestCacheItem>("member", "findname", "ka");
        }
    }

    [CacheContract]
    class TestCacheItem
    {
        [CacheMember]
        public string Name { get; set; }
        [CacheMember]
        public int? Age { get; set; }
        public DateTime? Birth { get; set; }
    }
}
